//
//  LECommitListController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LECommitListController.h"
#import "LECommitCell.h"
#import "LECommitModel.h"
#import "LEExplainContentController.h"

static NSString *const LECommitCellId = @"LECommitCell";

@interface LECommitListController ()

@end

@implementation LECommitListController

- (void)setupUI
{
    [super setupUI];
    self.title = @"评价列表";
    [self.tableView registerNib:[UINib nibWithNibName:@"LECommitCell" bundle:nil] forCellReuseIdentifier:LECommitCellId];
    [self initHeader];
    [self initFooter];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(explainNotifaction:) name:EXPLAIN_COMMIT_NOTIFICATION object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar setHidden:YES];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:ServiceCommitListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [self addNoDataViewWithTipText:error];
            }
        }else if ([datas isKindOfClass:[NSArray class]])
        {
            [self hiddenNoDataView];
            NSArray *tempModel = [LECommitModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            [weakSelf.tableView reloadData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LECommitCell *cell = [tableView dequeueReusableCellWithIdentifier:LECommitCellId forIndexPath:indexPath];
    __weak typeof (self) weakSelf = self;
    [cell commitCellClick:^(LECommitModel *model) {
        LEExplainContentController *explain = [[LEExplainContentController alloc] init];
        explain.model = model;
        explain.explainMode = Commit;
        [weakSelf.navigationController pushViewController:explain animated:YES];
    }];
    cell.model = self.dataSource[indexPath.row];
    cell.model.indexPath = indexPath;
    return cell;
}
#pragma mark- 点击事件
- (void)explainNotifaction:(NSNotification *)notification
{
    LECommitModel *model = notification.object;
    [self.tableView reloadRowsAtIndexPaths:@[model.indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
@end




















